﻿#include <GATE.HPP>

/*******************************
**         Win32 的實作       **
*******************************/
#if (SysSelect == _WIN32_CxxlMan)

#include <windows.h>

class ImpGateBase:public CxxlMan::IGateBase
{
  HANDLE hGate;
public:

  ImpGateBase(void)
  {
    hGate = CreateEvent(NULL, false, false, NULL);
  }

  ~ImpGateBase(void)
  {
    CloseHandle(hGate);
  }
  void cxxlFASTCALL Wait(void) const
  {
    WaitForSingleObject(hGate,INFINITE);
  }
  void cxxlFASTCALL Signal(void) const
  {
    SetEvent(hGate);
  }
};

CxxlMan::Gate::Gate()
{
  Imp = new ImpGateBase;
}



/*******************************
**         Linux 的實作       **
*******************************/
#elif (SysSelect == _LINUX_CxxlMan)

#include <pthread.h>

class ImpGateBase:public CxxlMan::IGateBase
{
  pthread_mutex_t mutex;
  pthread_cond_t cond;
public:

  ImpGateBase(void)
  {
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);
  }

  ~ImpGateBase(void)
  {
    while(pthread_cond_destroy(&cond) == EBUSY);

    DeleteCriticalSection(&mutex);
  }
  void cxxlFASTCALL Wait(void) const
  {
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond,&mutex);
    pthread_mutex_unlock(&mutex);
  }
  void cxxlFASTCALL Signal(void) const
  {
    pthread_cond_signal(&cond);
  }
};

CxxlMan::Gate::Gate()
{
  Imp = new ImpGateBase;
}

#else
  #error 尚未支援此作業系統
#endif

